//
//  StudyFourViewController.m
//  TeacherNet
//
//  Created by slz on 2017/12/18.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "StudyFourViewController.h"
#import "StudyFourTableViewCell.h"
#import "StudyTopicPageModel.h"
#import "NewWebViewController.h"
#import "UserNoteContenViewController.h"

@interface StudyFourViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) YWTableView *tableView;
@property (nonatomic,strong) StudyTopicPageModel *pageModel;
@end

@implementation StudyFourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"全部课程"];
    switch (_pageType) {
        case StudyPageTopicTypeHomeWork:
            [self setControllerTitle:@"我的作业"];
            break;
        case StudyPageTopicTypeExam:
            [self setControllerTitle:@"我的考试"];
            break;
        case StudyPageTopicTypeNote:
            [self setControllerTitle:@"我的笔记"];
            break;
        case StudyPageTopicTypeTopic:
            [self setControllerTitle:@"我的话题"];
            break;
        default:
            [self setControllerTitle:@"我的话题"];
            break;
    }
    UIBarButtonItem * backItem = [UIBarButtonItem itemWithImage:@"back_icon" selectImage:@"" addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = backItem;
    [self initSubView];
    [_tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear: animated];
//
//    // 马上进入刷新状态
//    if (_pageModel == nil){
//        [self.tableView.mj_header beginRefreshing];
//    }
//}

- (void)initSubView
{
    @weakify(self);
    _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self requestData];
    }];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}

- (void)requestData
{
    
    NSString * requestUrl;
    NSDictionary * parms;
    MFWUserInfo * user = [MFWSdkEnvironment sharedInstance].userInfo;
    switch (_pageType) {
        case StudyPageTopicTypeHomeWork:
            requestUrl = kAPI_STUDY_HOMEWORK_EXMA;
            parms = @{@"user_id":user.uid,@"type":@"1"};
            break;
        case StudyPageTopicTypeExam:
            requestUrl = kAPI_STUDY_HOMEWORK_EXMA;
            parms = @{@"user_id":user.uid,@"type":@"0"};
            break;
        case StudyPageTopicTypeNote:
            requestUrl = kAPI_STUDY_USER_NOTE;
            parms = @{@"user_id":user.uid};
            break;
        case StudyPageTopicTypeTopic:
            requestUrl = kAPI_STUDY_USER_TOPIC;
            parms = @{@"user_id":user.uid};
            break;
        default:
            requestUrl = kAPI_STUDY_HOMEWORK_EXMA;
            parms = @{@"user_id":user.uid,@"type":@"1"};
            break;
    }
    [self requestWithURL:requestUrl andParams:parms];
}

- (void)requestWithURL:(NSString *)url andParams:(NSDictionary *)params
{
    [BaseHttpTool get:url params:params success:^(id responseObj) {
        [_tableView.mj_header endRefreshing];
        NSDictionary * dict = [[MFWJson jsonWithObject:responseObj] toDic];
        _pageModel = [StudyTopicPageModel yy_modelWithDictionary:dict];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        [self.view showTip:error.localizedDescription];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (_pageType) {
        case StudyPageTopicTypeHomeWork:
            return _pageModel.homeworkOrExamList.count;
            break;
        case StudyPageTopicTypeExam:
            return _pageModel.homeworkOrExamList.count;
            break;
        case StudyPageTopicTypeNote:
            return _pageModel.noteList.count;
            break;
        case StudyPageTopicTypeTopic:
            return _pageModel.topicList.count;
            break;
        default:
            return _pageModel.homeworkOrExamList.count;
            break;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(_pageType == StudyPageTopicTypeTopic){
        NSString * urlHost = kAPI_HOST(@"/edu_yun/course/read_question_m.action");
        StudyHomeWorkOrExamModel * model = _pageModel.topicList[indexPath.row];
        NSString * url = [NSString stringWithFormat:@"%@?question_id=%@&user_id=%@",urlHost,model.topicId,[MFWSdkEnvironment sharedInstance].userInfo.uid];
        NewWebViewController * wenController = [[NewWebViewController alloc]init];
        wenController.url = url;
        [self.navigationController pushViewController:wenController animated:YES];
        return;
    }
    if (_pageType == StudyPageTopicTypeHomeWork || _pageType == StudyPageTopicTypeExam){
        NSString * urlHost = kAPI_HOST(@"/edu_yun/course/to_study_m.action");
        StudyHomeWorkOrExamModel * model = _pageModel.homeworkOrExamList[indexPath.row];
        NSString * url =  [NSString stringWithFormat:@"%@?id=%@&class_id=%@&user_id=%@",urlHost,model.courseId,model.classId,[MFWSdkEnvironment sharedInstance].userInfo.uid];
        NewWebViewController * wenController = [[NewWebViewController alloc]init];
        wenController.url = url;
        [self.navigationController pushViewController:wenController animated:YES];
        return;
    }
    if (_pageType == StudyPageTopicTypeNote){
        StudyHomeWorkOrExamModel * model = _pageModel.noteList[indexPath.row];
        TTCourseNoteModel * noteModel =[[TTCourseNoteModel alloc]init];
        noteModel.noteContent = model.noteName;
        noteModel.date = model.time;
        UserNoteContenViewController * noteController = [[UserNoteContenViewController alloc]init];
        noteController.noteModel = noteModel;
        [self.navigationController pushViewController:noteController animated:YES];
        return;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * resuerIndentifier = @"StudyFourTableViewCell";
    StudyFourTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:resuerIndentifier];
    if (!cell){
        cell = [[StudyFourTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resuerIndentifier];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    }
    StudyHomeWorkOrExamModel * model;
    switch (_pageType) {
        case StudyPageTopicTypeHomeWork:
            model = _pageModel.homeworkOrExamList[indexPath.row];
            break;
        case StudyPageTopicTypeExam:
            model = _pageModel.homeworkOrExamList[indexPath.row];
            break;
        case StudyPageTopicTypeNote:
            model = _pageModel.noteList[indexPath.row];
            break;
        case StudyPageTopicTypeTopic:
            model = _pageModel.topicList[indexPath.row];
            break;
        default:
            model = _pageModel.homeworkOrExamList[indexPath.row];
            break;
    }
    [cell layoutWithState:_pageType andModel:model];
    return cell;
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
